package com.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.mall.product.entity.AttrAttrgroupRelationEntity;
import com.mall.product.entity.AttrEntity;
import com.mall.product.service.AttrAttrgroupRelationService;
import com.mall.product.service.AttrService;
import com.mall.product.vo.AttrGroupRelationVo;
import com.mall.product.vo.AttrGroupVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.AttrGroupService;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.R;


/**
 * 属性分组
 *
 * @author super
 * @email 3217985254@qq.com
 * @date 2023-07-16 17:13:15
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     * {menuId} 为路径变量
     * 使用 @PathVariable("menuId") Long catelogId 获取值
     */
    @RequestMapping("/list/{menuId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("menuId") Long menuId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, menuId);


        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
//		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        AttrGroupEntity attrGroup = attrGroupService.getAttrGroupById(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @RequestMapping("/{attrGroupId}/attr/relation")
    public R getAllAttrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> attrEntity = attrService.getAllByGroupId(attrGroupId);
        return R.ok().put("data", attrEntity);
    }

    @RequestMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos) {
        attrGroupService.deleteAttrRelation(attrGroupRelationVos);
        return R.ok();
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     */
    @RequestMapping(value = "/{attrgroupId}/noattr/relation", method = RequestMethod.GET)
    public R getNoAttrList(@PathVariable("attrgroupId") Long attrGroupId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.getNoAttrList(attrGroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * 添加属性与分组关联关系
     *
     * @param attrGroupRelationVo
     * @return
     */
    @RequestMapping("/attr/relation")
    private R relationAttr(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVo) {
        List<AttrAttrgroupRelationEntity> collect = attrGroupRelationVo.stream().map((item) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationService.saveBatch(collect);
        return R.ok();
    }
    /**
     * 获取分类下所有分组&关联属性
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getWithattrByCatelogId(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupVo> attrGroupVos = attrGroupService.getWithattrByCatelogId(catelogId);
        return R.ok().put("data",attrGroupVos);
    }

}
